<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>axios创建实例对象发送请求</title>
    <link
      crossorigin="anonymous"
      href="https://cdn.bootcss.com/twitter-bootstrap/3.3.7/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.min.js"></script>
  </head>
  <body>
    <div class="container">
      <h2 class="page-header">基本使用</h2>
      <button class="btn btn-primary">发送GET请求 获取数据</button>
      <button class="btn btn-warning">发送POST请求 上传数据</button>
      <button class="btn btn-success">发送 PATCH 请求 更新指定数据</button>
      <button class="btn btn-danger">发送 DELETE 请求 删除指定数据</button>
    </div>
    <script>
      // 获取按钮
      const btns = document.querySelectorAll("button");

      // 创建实例对象
      const potos = axios.create({
        // 默认配置
        baseURL: "http://localhost:3000",
        timeout: "3000",
        params: {
          id: 2,
        },
      });

      // 为不同的域名创建不同的实例对象
      const onathor = axios.create({
        baseURL: "http://athor:3000",
        timeout: "2000",
      });

      // 这里 potos 对象与 axios 对象功能几乎一样
      console.log(potos);

      // 使用实例对象发送请求
      potos({
        url: "/posts",
      }).then((response) => {
        console.log(response);
      });

      potos.get("/posts").then((response) => {
        console.log(response.data[0]);
      });
    </script>
  </body>
</html>


